package com.ruoyi.project.module.releaseGroup.service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.application.domain.Application;
import com.ruoyi.project.module.applicationProperty.domain.ApplicationProperty;
import com.ruoyi.project.module.releaseProperty.domain.ReleaseProperty;
import com.ruoyi.project.module.releaseProperty.service.IReleasePropertyService;
import com.ruoyi.project.module.task.mapper.TaskUtilsMapper;
import org.shoukaiseki.koori.model.ResultKoori;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.releaseGroup.mapper.ReleaseGroupMapper;
import com.ruoyi.project.module.releaseGroup.domain.ReleaseGroup;
import com.ruoyi.project.module.releaseGroup.service.IReleaseGroupService;
import com.ruoyi.common.support.Convert;
import javax.annotation.Resource;

/**
 * 发布属性组 服务层实现
 * 
 * @author ruoyi
 * @date 2018-12-06
 */
@Service
public class ReleaseGroupServiceImpl implements IReleaseGroupService 
{

	@Resource
	private TaskUtilsMapper taskUtilsMapper;
	@Resource
	private ReleaseGroupMapper releaseGroupMapper;

	@Resource
	private IReleasePropertyService releasePropertyService;

	/**
     * 查询发布属性组信息
     * 
     * @param configReleaseGroupId 发布属性组ID
     * @return 发布属性组信息
     */
    @Override
	public ReleaseGroup selectReleaseGroupById(Long configReleaseGroupId)
	{
	    return releaseGroupMapper.selectReleaseGroupById(configReleaseGroupId);
	}
	
	/**
     * 查询发布属性组列表
     * 
     * @param releaseGroup 发布属性组信息
     * @return 发布属性组集合
     */
	@Override
	public List<ReleaseGroup> selectReleaseGroupList(ReleaseGroup releaseGroup)
	{
	    return releaseGroupMapper.selectReleaseGroupList(releaseGroup);
	}
	
    /**
     * 新增发布属性组
     * 
     * @param releaseGroup 发布属性组信息
     * @return 结果
     */
	@Override
	public int insertReleaseGroup(ReleaseGroup releaseGroup)
	{
		Date currentDate = taskUtilsMapper.getCurrentDate();
		String userName = ShiroUtils.getUser().getLoginName();
		releaseGroup.setCreateBy(userName);
		releaseGroup.setCreateTime(currentDate);
		releaseGroup.setUpdateBy(userName);
		releaseGroup.setUpdateTime(currentDate);
	    return releaseGroupMapper.insertReleaseGroup(releaseGroup);
	}
	
	/**
     * 修改发布属性组
     * 
     * @param releaseGroup 发布属性组信息
     * @return 结果
     */
	@Override
	public int updateReleaseGroup(ReleaseGroup releaseGroup)
	{
		Date currentDate = taskUtilsMapper.getCurrentDate();
		String userName = ShiroUtils.getUser().getLoginName();
		releaseGroup.setCreateBy(null);
		releaseGroup.setCreateTime(null);
		releaseGroup.setUpdateBy(userName);
		releaseGroup.setUpdateTime(currentDate);
	    return releaseGroupMapper.updateReleaseGroup(releaseGroup);
	}

	/**
     * 删除发布属性组对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteReleaseGroupByIds(String ids)
	{
		return releaseGroupMapper.deleteReleaseGroupByIds(Convert.toStrArray(ids));
	}
	@Override
	public ResultKoori release(Application configApplication){
		Date currentDate = taskUtilsMapper.getCurrentDate();
		ResultKoori resultKoori=new ResultKoori();
		ReleaseGroup releaseGroupFindTemp = new ReleaseGroup();
		releaseGroupFindTemp.setConfigApplicationId(configApplication.getConfigApplicationId().longValue());
		List<ReleaseGroup> releaseGroups = selectReleaseGroupList(releaseGroupFindTemp);
		ReleaseGroup releaseGroup;
		if(releaseGroups.isEmpty()){
			releaseGroup=new ReleaseGroup();
			BeanUtils.copyProperties(configApplication,releaseGroup);
			releaseGroup.setConfigApplicationId(configApplication.getConfigApplicationId().longValue());
			releaseGroup.setReleaseTime(currentDate);
			releaseGroup.setMaxVersion(1);
			releaseGroup.setCurrentVersion(1);
			insertReleaseGroup(releaseGroup);
			resultKoori.setCurrentVersion(1);
		}else{
			releaseGroup = releaseGroups.stream().findFirst().get();
			releaseGroup.setReleaseTime(currentDate);
			updateReleaseGroup(releaseGroup);
			releaseGroup = selectReleaseGroupById(releaseGroup.getConfigReleaseGroupId());
			resultKoori.setCurrentVersion(releaseGroup.getCurrentVersion());
		}

		for (ApplicationProperty applicationProperty:configApplication.getConfigApplicationPropertyList()) {
			ReleaseProperty releaseProperty=new ReleaseProperty();
			BeanUtils.copyProperties(applicationProperty,releaseProperty);
			releaseProperty.setConfigReleaseGroupId(releaseGroup.getConfigReleaseGroupId());
			releaseProperty.setCurrentVersion(releaseGroup.getCurrentVersion());
			releasePropertyService.insertReleaseProperty(releaseProperty);
		}

		return resultKoori;
	}

}
